using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;

namespace Vitus.MediaProcessing
{
    public class Migrations : DataMigrationImpl
    {
        public int Create()
        {
            SchemaBuilder.CreateTable("MediaProfileAttachmentsPartRecord", table => table
                .ContentPartRecord()
                );

            SchemaBuilder.CreateTable("MediaProfileAttachmentRecord", table => table
                .Column<int>("Id", c => c.PrimaryKey().Identity())
                .Column<int>("MediaProfileAttachmentsPartRecord_Id")
                // MediaFolderPath, MediaFilename and ProfileName are used instead of Ids because they are part of
                // profile image urls which are hardcoded into the HTML where the attachments
                // are used. Any change of these values invalidates the HTML since we cannot 
                // auto-correct the HTML upon change. Therefore, we may save some database 
                // queries at least.
                .Column<string>("MediaPath", c => c.WithLength(4096))
                .Column<string>("ProfileName", c => c.WithLength(255))
                );

            SchemaBuilder.AlterTable("MediaProfileAttachmentRecord", table => table
                .CreateIndex("IX_MediaProfileAttachmentsPartRecord_Id", "MediaProfileAttachmentsPartRecord_Id")
                );
            
            ContentDefinitionManager.AlterPartDefinition("MediaProfileAttachmentsPart", builder => builder
                .WithDescription("Allows attaching media in specific profiles to the content so that it is always available when the content is displayed.")
                .Attachable());

            return 1;
        }
    }
}